package com.app.business.utils;

import com.app.business.model.HousePersonForProperty;
import com.app.business.service.HousePersonForPropertyService;
import com.app.business.service.HouseService;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.ApplicationContextProvider;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import org.springframework.context.ApplicationContext;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 物业减免人员登记限制
 * Created by ddz on 2022/3/16.
 */
@SuppressWarnings("all")
public class PropertyPersonUtils {
    private static HouseService houseService;
    private static HousePersonForPropertyService housePersonForPropertyService;
    private static DataDictService dataDictService;

    public static Response getPropertyPersonLimit(HousePersonForProperty housePersonForPropertyNew) {
        ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
        houseService = (HouseService) applicationContext.getBean(HouseService.class);
        housePersonForPropertyService = (HousePersonForPropertyService) applicationContext.getBean(HousePersonForPropertyService.class);
        dataDictService = (DataDictService) applicationContext.getBean(DataDictService.class);
        Map<String, DataDict> housePersonForPropertyMap = dataDictService.getMapByParentNameForValue("物业减免类型");

        //该身份证号已减免面积
        BigDecimal allArea = null;
        String errorStr = "";
        //当前录入最新减免数据减免时间
        Date startDateNew = housePersonForPropertyNew.getStartDate();
        Precondition.checkAjaxArguement(startDateNew != null, "1111", "请录入减免开始时间！");

        //结束减免时间，如果为空，则自动延续10年，为结束减免时间
        Date stopDateNew = housePersonForPropertyNew.getStopDate() == null ?
                DateUtils.getDateByDateAndYears(startDateNew, 10) : housePersonForPropertyNew.getStopDate();
        //当前身份证号所有减免数据
        List<HousePersonForProperty> housePersonForPropertyList = housePersonForPropertyService.getHousePersonListByIdCard(housePersonForPropertyNew.getIdCard());
        if (!CollectionUtils.isEmpty(housePersonForPropertyList)) {
            for (HousePersonForProperty housePersonForPropertyOld : housePersonForPropertyList) {

                //如果原有的减免数据中减免结束时间等于空
                //原有录入减免数据减免时间
                Date startDateOld = housePersonForPropertyOld.getStartDate();
                //原有录入减免数据，结束减免时间，如果为空，则自动延续10年，为结束减免时间
                Precondition.checkAjaxArguement(startDateOld != null, "1111", "数据未录入减免登记时间！"+housePersonForPropertyOld.getHouse().getAzProjectNameAndHouseNumStr());

                Date stopDateOld = housePersonForPropertyOld.getStopDate() == null ?
                        DateUtils.getDateByDateAndYears(startDateOld, 10) : housePersonForPropertyOld.getStopDate();
                //判断新录入的减免时间是否在已有数据时间范围内，不在则继续。
                //新减免数据开始时间大于旧减免数据结束时间  或者   新减免数据结束时间小于旧减免数据开始时间
                //除了此二种情况 说明新的减免数据不在旧的减免数据时间范围内，其余皆有重合
                if (startDateNew.compareTo(stopDateOld) > 0 || stopDateNew.compareTo(startDateOld) < 0) {
                    continue;
                } else {
                    //如果存在不同补助标准，则返回已输入标准错误信息

                    errorStr = errorStr + "</br >" + housePersonForPropertyOld.getHouse().getAzProjectNameAndHouseNumStr() + housePersonForPropertyOld.getName() + ";";
                    allArea = BigDecimalUtils.getSum(allArea, housePersonForPropertyOld.getFreeArea());
                    if (housePersonForPropertyNew.getHouse().getContract().getPropertyType() !=
                            housePersonForPropertyOld.getHouse().getContract().getPropertyType()) {
                        return new Response("0000", "当前存在多种减免标准，请核对后进行录入减免！" + errorStr);
                    }
                }
            }
            Precondition.checkAjaxArguement(housePersonForPropertyNew.getHouse().getContract().getPropertyType() != null, "1111", "减免标准为空，请联系技术支持调整！");

            DataDict dataDictNew = housePersonForPropertyMap.get(housePersonForPropertyNew.getHouse().getContract().getPropertyType().toString());
            if (allArea.compareTo(BigDecimalUtils.stringToBigDecimal(dataDictNew.getMemo())) > 0) {
                return new Response("0000", "减免面积已超出最大范围，请核对后填写！" + errorStr);
            }

        }
        return new Response("0001", "验证成功！");

    }

}